﻿using System;
using System.Globalization;
using System.Xml.Serialization;
using SS.Common.Logging;

namespace SS.Common.Map.CountryDetect
{
    public class Response
    {
        private static readonly ILogger Logger = LoggerManager.GetLogger(typeof(Response).Name);

        [XmlElement("Ip")]
        public string Ip { get; set; }

        [XmlElement("CountryCode")]
        public string CountryCode { get; set; }

        [XmlElement("CountryName")]
        public string CountryName { get; set; }

        [XmlElement("RegionCode")]
        public string RegionCode { get; set; }

        [XmlElement("RegionName")]
        public string RegionName { get; set; }

        [XmlElement("City")]
        public string City { get; set; }

        [XmlElement("ZipCode")]
        public string ZipCode { get; set; }

        [XmlElement("Latitude")]
        public string Latitude { get; set; }

        [XmlElement("Longitude")]
        public string Longitude { get; set; }

        [XmlElement("MetroCode")]
        public string MetroCode { get; set; }

        [XmlElement("AreaCode")]
        public string AreaCode { get; set; }

        public string LanguageName
        {
            get
            {
                var rs = "vi";
                foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
                {
                    var ri = new RegionInfo(info.Name);
                    if (ri.DisplayName.Equals(this.CountryName, StringComparison.OrdinalIgnoreCase))
                    {
                        rs = info.TwoLetterISOLanguageName;
                        break;
                    }
                }
                Logger.InfoFormat("DetectCountry {0} => LanguageName {1}", this.CountryName, rs);
                return rs;
            }
        }
    }
}
